<template>
  <view class="search-page">
    <view class="search-head" :style="'padding-top:' + offsetTop + 'rpx'">
      <up-search placeholder="搜索藏品名称或关键词" focus clearabled v-model="keyword" actionText="取消" @custom="oncustom"
        @search="onsearch"></up-search>
    </view>
    <view class="search-record">
      <view class="record-head" v-if="historyList.length > 0">
        <view class="head-title">历史搜索</view>
        <up-icon name="trash" size="32rpx" color="#999999" @click="onclear"></up-icon>
      </view>
      <view class="record-list" v-if="historyList.length > 0">
        <view class="record-nav" v-for="item,index in historyList" :key="index" @click="onsearch(item)">{{item}}</view>
      </view>
      <!-- <view class="record-head">
        <view class="head-title">热门搜索</view>
      </view> -->
      <!-- <view class="record-list">
        <view class="record-nav" v-for="item,index in popularList" :key="index" @click="onsearch(item)">{{item}}</view>
      </view> -->
    </view>
  </view>
</template>

<script setup>
  import {
    ref
  } from 'vue'
  import {
    onLoad,
    onShow
  } from '@dcloudio/uni-app'
  const keyword = ref('')
  const recordShow = ref(false)
  const historyList = ref([])
  const popularList = ref(["纸币"])
  const offsetTop = ref(0)
  // #ifdef APP-PLUS
  let topHight = uni.getSystemInfoSync().statusBarHeight
  offsetTop.value = topHight + 64
  // #endif
  onShow(() => {
    const history = uni.getStorageSync('searchHistory');
    historyList.value = history ? history : [];
  })

  function oncustom() {
    uni.navigateBack(-1)
  }

  function onclear() {
    uni.showModal({
      content: '确定删除全部历史记录?',
      success(res) {
        if (res.confirm) {
          historyList.value = []
          uni.removeStorageSync('searchHistory')
        }
      }
    })
  }

  function onsearch(val) {
    const searchHistory = []
    const index = historyList.value.indexOf(val);
    if (index !== -1) {
      historyList.value.splice(index, 1);
    }
    historyList.value.unshift(val);
    uni.setStorageSync('searchHistory', historyList.value)
    uni.navigateTo({
      url: `/pages/index/searchPage/searchResult?keyword=${val}`
    })
    keyword.value = ''
  }
</script>

<style lang="scss" scoped>
  .search-page {
    height: 100%;
    display: flex;
    flex-direction: column;

    .search-head {
      width: 750rpx;
      height: 88rpx;
      background: rgba(255, 255, 255, 1);
      box-sizing: border-box;
      padding: 0 30rpx;
      flex: 0 0 auto;
      display: flex;
      align-items: center;
      padding-top: var(--status-bar-height);
      position: sticky;
      top: 0;
      left: 0;
      z-index: 10;
    }

    .search-record {
      flex: 1;
      display: flex;
      flex-direction: column;
      box-sizing: border-box;
      padding: 0 30rpx;
      background-color: #fff;

      .record-head {
        margin-top: 48rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 34rpx;
        font-weight: 500;
        color: rgba(51, 51, 51, 1);

        .head-title {
          font-size: 34rpx;
          font-weight: 600;
          color: rgba(51, 51, 51, 1);
          position: relative;
          width: max-content;
          z-index: 1;
          display: flex;
        }

        .head-title:before {
          content: "";
          width: 100%;
          height: 16rpx;
          opacity: 0.5;
          background: linear-gradient(90deg, $color 0%, transparent 100%);
          position: absolute;
          bottom: 0;
          left: 0;
          z-index: 0;
        }
      }

      .record-list {
        display: flex;
        flex-wrap: wrap;
        margin-top: 40rpx;

        .record-nav {
          height: 56rpx;
          opacity: 1;
          border-radius: 342rpx;
          line-height: 56rpx;
          background: #F2F2F2;
          box-sizing: border-box;
          padding: 0 32rpx;
          margin: 0 24rpx 24rpx 0;
          font-size: 28rpx;
          font-weight: 400;
          color: rgba(51, 51, 51, 1);

        }
      }

    }

    .search-nav {
      display: flex;
      flex: 1;
      flex-direction: column;
      overflow: scroll;

      .nav-head {
        width: 100%;
        height: 88rpx;
        display: flex;
        align-items: center;
        flex: 0 0 auto;

        .head-tab {
          flex: 1;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 28rpx;
          font-weight: 400;
          color: rgba(102, 102, 102, 1);

        }

        .tab-active {
          flex: 1;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 28rpx;
          font-weight: 500;
          color: rgba(51, 51, 51, 1);

        }
      }

      .nav-list {
        margin: 24rpx 0;
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        box-sizing: border-box;
        padding: 0 30rpx;
        overflow: scroll;

        .nav-content {
          width: 336rpx;
          height: 464rpx;
          border-radius: 16rpx;
          background: #FFFFFF;
          margin-bottom: 20rpx;
          overflow: hidden;

          .content-img {
            width: 336rpx;
            height: 336rpx;
            position: relative;

            .img-mask {
              width: 336rpx;
              height: 336rpx;
              position: absolute;
              top: 0;
              left: 0;
              background: rgba(0, 0, 0, 0.6);
              opacity: 0.6;
              display: flex;
              flex-direction: column;
              align-items: center;
              justify-content: center;
              z-index: 1;

              .mask-title {
                font-size: 28rpx;
                font-weight: 400;
                color: rgba(255, 255, 255, 1);
                margin-top: 12rpx;
              }
            }

            .image {
              width: 336rpx;
              height: 336rpx;
            }
          }

          .content-info {
            display: flex;
            flex-direction: column;
            box-sizing: border-box;
            padding: 20rpx;
            position: relative;

            .info-status {
              position: absolute;
              right: 16rpx;
              top: -46rpx;
              height: 96rpx;
              width: 96rpx;
              border-radius: 50%;
              z-index: 1;
              background-color: #FFFFFF;
              display: flex;
              align-items: center;
              justify-content: center;

              image {
                height: 80rpx;
                width: 80rpx;
              }
            }

            .info-head {
              display: flex;
              align-items: center;

              .head-avatar {
                height: 36rpx;
                width: 36rpx;
                border-radius: 50%;
              }

              .head-name {
                font-size: 24rpx;
                font-weight: 400;
                color: rgba(102, 102, 102, 1);
                margin-left: 8rpx;
              }
            }

            .info-title {
              font-size: 26rpx;
              font-weight: 400;
              color: rgba(51, 51, 51, 1);
              margin-top: 12rpx;
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
            }
          }
        }
      }
    }
  }
</style>